<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Ad;
use app\common\model\GoodsRecord;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
    	$returnData = [
    	    'banner' => [],
		    'category' => [],
		    'new_revealed' => [],
	    ];
	
    	$adModel = new Ad();
    	$returnData['banner'] = $adModel->getAdById(11);
	    $returnData['category'] = $adModel->getAdById(12);
	    
	    $recordModel = new GoodsRecord();
	    $record = Db::name('GoodsRecord')->alias('r')
		    ->join('goods g', 'r.goods_id = g.id', 'left')
		    ->where('r.status', '<>', $recordModel::STATUS_B)
		    ->order(['r.update_time' => 'DESC'])
		    ->limit(12)
		    ->field('r.id, g.goods_name, g.image, r.status, r.open_time')
		    ->select();
	    
	    if($record) {
	    	$statusList = $recordModel::$statusList;
	    	foreach ($record as $k => $v) {
	    		$v['status_text'] = __($statusList[$v['status']]);
	    		$v['open_datetime'] = date('Y-m-d H:i:s', $v['open_time']);
			    $record[$k] = $v;
	    		
		    }
		    $returnData['new_revealed'] = $record;
	    }
	    
    	$this->success(__('查询成功'), $returnData);
    }

}
